<%-- 
    Document   : commentEvent
    Created on : 16-mar-2014, 15.25.46
    Author     : Mark
--%>


<%@page import="it.evento.utils.genpage.Article"%>
<%@page import="it.evento.utils.genpage.ArticleRow"%>
<%@page import="it.evento.utils.genpage.RowCell"%>
<%@page import="it.evento.Users"%>
<%@page import="it.evento.utils.genpage.BlogPost"%>

<%@page import="it.evento.BusinessLogic" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    boolean isLoggedUser = false;
    Users loggedUser = null;
    
    if(session.getAttribute("loggedUser")!=null) {
        loggedUser = (Users)session.getAttribute("loggedUser");
        isLoggedUser = true;
    }
    else {
        isLoggedUser = false;
    }
%>    

<!DOCTYPE html>
<html dir="ltr" lang="it-IT">
    <head>
        <title>EvenTO</title>

        <jsp:include page="html/metadata.jsp"/>
        
        <script>
            $(function() {      
                $.getJSON( "Controller?type=getAllEvents", function( data ) {
                    var items = [];
                    $.each( data, function(key,val) {
                    items.push("<option value='" + val.id + "'>" + val.label + "</option>");
                });
                    
                /*Aggiunge la select la fondo del body*/
                $( "<select/>", {
                    "name":"selectedEvents",
                    html: items.join( "" )
                    }).appendTo( "#blog" );
                });
                    
                   
                 /* INSERT COMMENT */
                     
                $('#insertComment').click(function(e) {
                e.preventDefault();
                    
                if($('input[name="commentTxt"]').val() === ""){
                        $('#msgOut').html("<ul><li>Inserisci il tuo commento</li></ul>");
                    } else {
                        $.post("Controller",
                                {type: "insertComment",
                                    idEvent: $('select[name="selectedEvents"]').val(),
                                    comment: $('input[name="commentTxt"]').val()
                                },
                        function(data) {
                            if (data.status === 0) {
                                $('#msgOut').html("<ul>" + data.message+"</ul>");
                                window.location.replace("/ProgettoEvenTo-war/index.jsp"); //spara utente in index
                            }
                            else {
                                $('#msgOut').html("<ul>" + data.status + ": " + data.message+"</ul>");
                            }
                        },
                                'json');
                    }


                });
                /* /INSERT COMMENT */

            });
        </script>

    </head>
    <body>
        <div id="evento-main">
            <!-- header -->
            <jsp:include page="html/header.jsp" />
            <!-- /header -->

            <!-- top-menu -->
            <%@include file="html/top-menu.jsp" %>
            <!-- /top-menu -->

            <div class="evento-sheet clearfix">
                <div class="evento-layout-wrapper">
                    <div class="evento-content-layout">
                        <div class="evento-content-layout-row">
                            <div class="evento-layout-cell evento-content">
                                <% if(isLoggedUser) { %>
                                    <%=
                                        new BlogPost()
                                                .setTitle("Comment Events")
                                                .setContent("<h3>Lascia un commento, il tuo parere è importante!</h3>"+"<br>"
                                                        + "Select Event :<div id='blog'></div>"
                                                        + "<br>Tuo commento: <br> <input type='text' name='commentTxt'/>"
                                                        + "<input type='button' id='insertComment' value='Commenta' />"
                                                        + "<div id='msgOut'></div>")
                                                .print()
                                    %>          
                                <% }else{ %>
                                        <div align="center"><h2>Per vedere il contenuto della pagina devi fare il login</h2></div>
                                <%} %>
                                
           
                            </div>

                            <!-- sidebar -->
                            <jsp:include page="html/sidebar.jsp" />
                            <!-- /sidebar -->
                            
                        </div>
                    </div>
                </div>

                <!-- footer -->
                <%@include file="html/footer.jsp" %>
                <!-- /footer -->

            </div>
        </div>
    </body>
</html>
